package yuan.zk;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.concurrent.CountDownLatch;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/09/11 13:39
 */


public class ZookeeperConnection {

    public static void main(String[] args) {
        try {
            CountDownLatch latch = new CountDownLatch(1);
            ZooKeeper zooKeeper = new ZooKeeper("192.168.190.128:2181", 5000, new Watcher() {
                @Override
                public void process(WatchedEvent watchedEvent) {
                    if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                        System.out.println("connection create successfully");
                        latch.countDown();
                    }
                }
            });

            latch.await();
            System.out.println(zooKeeper.getSessionId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
